package cn.zgwj.pet.controller;

import cn.zgwj.basic.exception.BusinessException;
import cn.zgwj.basic.util.AjaxResult;
import cn.zgwj.basic.util.LoginContext;
import cn.zgwj.basic.util.PageList;
import cn.zgwj.pet.domain.Pet;
import cn.zgwj.pet.domain.SearchMasterMsg;
import cn.zgwj.pet.query.SearchMasterMsgQuery;
import cn.zgwj.pet.service.ISearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.queryAll();
    }

    /**
     * 发布寻主消息
     * @param masterMsg
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg masterMsg, HttpServletRequest request){
        try {
            seachMasterMsgService.publish(masterMsg, LoginContext.getCurrentLogininfo(request));
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    //1.用户查询自己的寻主消息
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        return seachMasterMsgService.user(query,LoginContext.getCurrentLogininfo(request));
    }


    //2.查询待处理寻主消息
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        query.setState(0);//待处理
        return seachMasterMsgService.pending(query,LoginContext.getCurrentLogininfo(request));
    }

    //3.查询已处理寻主消息
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request){
        query.setState(1);//待处理
        return seachMasterMsgService.processed(query,LoginContext.getCurrentLogininfo(request));
    }


    /**
     * 处理寻主消息
     * @param pet
     * @param request
     * @return
     */
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            seachMasterMsgService.handle(pet, LoginContext.getCurrentLogininfo(request));
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

}
